# -- coding:utf-8 --
# 解决 execjs 编码问题
# import subprocess
# from functools import partial
# subprocess.Popen = partial(subprocess.Popen, encoding="utf-8")

import requests
import execjs

url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36",
    "Cookie": "BAIDUID=69A2E2F81EC5D15399AD96A65C89EA6A:FG=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1655207188; APPGUIDE_10_0_2=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1655209176; ab_sr=1.0.1_MDdlOGU1Nzk0YzY2Mjc0NTI4NGQ2MTBhOWE3MTk0MDM1YjM5MWY2YWY0MmEzMmM3ZGQ1MDVjMDFlZWQxOTYwZWQ5NTk1M2I4OTI1YTc1OTQzY2NlODg3YWNhMzRhYWRmYWM3MDczNmJmYWI5YmJmMzFhMWIwNmI0YzNkOTYxZTkwNjM1YjkzYjQzMzcxMmI4ZTU3MzM1MjBjMDFiYzdkZg==",
}
params = {
    "from": "en",
    "to": "zh",
    "query": "test12123",
    "token": "abc2dafdf27b1540f5e3647fe4b15a66",
    "transtype": "translang",
    "simple_means_flag": "3",
    "domain": "common",
}
js_code = ''
with open(r'singFun.js', 'r') as f:
    js_code = f.read()

# 第一个参数是指定函数名称，后面的参数是函数的参数
params['sign'] = execjs.compile(js_code).call("e", params['query'])
res = requests.post(url, headers=headers, data=params).json()
print(res)
